import { toHttps } from './util';

function setSinger(singer) {
  const name = [];
  singer.forEach((item) => {
    name.push(item.name);
  });
  return name.join('/');
}
export class Song {
  constructor({
    id, name, album, time, songer, url, image,
  }) {
    this.id = id;
    this.name = name;
    this.album = album;
    this.time = time;
    this.songer = songer;
    this.url = url;
    this.image = image;
  }
}
export function createPlayList(music) {
  return new Song({
    id: music.id,
    name: music.name,
    album: music.al.name,
    songer: music.ar.length > 0 && setSinger(music.ar),
    image: toHttps(music.al.picUrl) || null,
    time: music.dt,
    url: `https://music.163.com/song/media/outer/url?id=${music.id}.mp3`,
  });
}

// 歌曲格式化
const formatSongs = function formatSong(musicdata) {
  const list = [];
  musicdata.forEach((item) => {
    if (item.id) {
      list.push(createPlayList(item));
    }
  });
  return list;
};

export default formatSongs;
